<?php
/*  psnFriends 0.9.2
 
    Copyright (C) 2011  Vespo Marco | http://thelairofthemad.altervista.org/

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

-----------------------------------------------------------------------------------------------*/
include_once 'psnRemoteFileReader.class.php';

class psnFriends{

	protected $xml,$psn,$json_string;
	
	function __construct($sessionId){
				
		$this->psn=new psnRemoteFileReader($sessionId);
		$this->psn->login();
		$this->psn->getData();
		$this->xml=$this->psn->getXml();				
	}
	
	public function echoXmlOnlineFriends(){
		$str='';
	   	foreach($this->xml->psn_friend as $friend)
	   		if($friend->current_presence=="online-ingame"||$friend->current_presence=="null")			
	       			$str=$this->echoXmlFriend($str,$friend);
	       	   
	       	return $str;  
	}
		
	public function echoXmlFriends(){
		$str='';
	   	foreach($this->xml->psn_friend as $friend)			
	       		$str=$this->echoXmlFriend($str,$friend);
	       	   
	       	return $str;  
	}
	
	protected function echoXmlFriend($str,$friend){
		$str.="<br>&nbsp;<img src='https://secure.eu.playstation.com".$friend->current_avatar."'>&nbsp;<b>".$friend->onlineid."</b><br>&nbsp;&nbsp;<div class='current_game'>".$friend->current_game."</div><br>&nbsp;level:".$friend->trophy->level."&nbsp;bronze:".$friend->trophy->bronze."&nbsp;silver:".$friend->trophy->silver."&nbsp;gold:".$friend->trophy->gold."&nbsp;platinum:".$friend->trophy->platinum."<br><br>";
		return $str;
	}
		
	public function getPsnCourrier(){return $this->psn;}
	
	public function getXml(){return $this->xml;}
	
	public function getJsonString(){return $this->json_string;}
	
	public function getPlayersPlay($game){
		$players= array();
		$i=0;
		foreach($this->xml->psn_friend as $friend){
			if($friend->current_game==$game)
				$players[$i++]=$friend;
		}
		return $players;
	}
	
	public function echoPlayersPlay($game){
		$str="";
		$players=$this->getPlayersPlay($game);
		foreach($players as $player)
			$str.="<image src='https://secure.eu.playstation.com".$player->current_avatar."'>&nbsp;<b>".$player->onlineid."</b><br>";		
		return $str;	
	}
	
	public function getPlayer($id){
		foreach($this->xml->psn_friend as $friend)
			if($friend->onlineid==$id)
				return $friend;
		return false;
	}
	
	public function makeOnlineJSONFromXml(){	
		$online= array();
		$i=0;			
		foreach($this->xml->psn_friend as $friend){
			if($friend->current_presence=="online-ingame"||$friend->current_presence=="null")
				$online[$i++]=array('username'=>''.$friend->onlineid,'currentGame'=>''.$friend->current_game,'avatar'=>'https://secure.eu.playstation.com'.$friend->current_avatar);		      	
		}	 
		$this->json_string = json_encode($online);
		return $this->json_string;	
	}
	
	public function makeJSONFromXml(){	
		$this->json_string = json_encode($this->loadXml());
		return $this->json_string;	
	}
}
?>
